package com.demo.lcgl.exception;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.server.NotAcceptableStatusException;
import org.springframework.web.servlet.NoHandlerFoundException;

import com.demo.lcgl.exception.code.Code;
import com.demo.lcgl.vo.ResponseEnvelope;

import lombok.extern.slf4j.Slf4j;

/**
 * @author wx
 */
@Slf4j
@RestControllerAdvice
@Order(value = Ordered.HIGHEST_PRECEDENCE)
public class GlobalExceptionHandler {

    @ExceptionHandler({LcglException.class, Exception.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseEntity<ResponseEnvelope<String>> handleException(Exception e) {
        log.error("系统内部异常，异常信息：", e);
        String message = e instanceof LcglException ? e.getMessage() : Code.C500.getDesc();
        return new ResponseEntity<>(new ResponseEnvelope(Code.C500.getCode(), message), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    public ResponseEntity<ResponseEnvelope<String>> handleHttpRequestMethodNotSupportedException(Exception e) {
        log.error("HTTP请求方式不被支持：", e);
        return new ResponseEntity<>(new ResponseEnvelope(Code.C405.getCode(), Code.C405.getDesc()), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(value = NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ResponseEntity<ResponseEnvelope<String>> handleNoHandlerFoundException(Exception e) {
        log.error("HTTP请求内容未找到：", e);
        return new ResponseEntity<>(new ResponseEnvelope(Code.C404.getCode(), Code.C404.getDesc()), HttpStatus.NOT_FOUND);
    }

    @ExceptionHandler(value = HttpMediaTypeNotSupportedException.class)
    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    public ResponseEntity<ResponseEnvelope<String>> handleHttpMediaTypeNotSupportedException(Exception e) {
        log.warn(e.getMessage());
        return new ResponseEntity<>(new ResponseEnvelope(Code.C415.getCode(), Code.C415.getDesc()), HttpStatus.UNSUPPORTED_MEDIA_TYPE);
    }

    @ExceptionHandler({NotAcceptableStatusException.class, HttpMediaTypeNotAcceptableException.class})
    @ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
    public ResponseEntity<ResponseEnvelope<String>> handleNotAcceptableException(Exception e) {
        log.warn(e.getMessage());
        return new ResponseEntity<>(new ResponseEnvelope(Code.C406.getCode(), Code.C406.getDesc()), HttpStatus.NOT_ACCEPTABLE);
    }
}
